#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include <cstring>
#include <cstdio>
#include <memory>
#include <vector>
#include <string>

#include "Thread.hpp"

int tickets = 10000;
void *getTicket(void *args)
{
    std::string username = static_cast<const char *>(args);
    while (true)
    {
        if (tickets > 0)
        {
            usleep(1234);
            // 可以抢
            std::cout << username << "正在进行抢票: " << tickets<< std::endl;
            tickets--;
            // 用这段时间来模拟真实的抢票要花费的时间
            //usleep(123); // 1秒=1000毫秒=1000 000微秒=10^9纳秒
        }
        else
        {
            break;
        }
    }
    return nullptr;
}

int main()
{
    std::unique_ptr<Thread> thread1(new Thread(getTicket, (void *)"user1", 1));
    std::unique_ptr<Thread> thread2(new Thread(getTicket, (void *)"user2", 2));
    std::unique_ptr<Thread> thread3(new Thread(getTicket, (void *)"user3", 3));
    std::unique_ptr<Thread> thread4(new Thread(getTicket, (void *)"user4", 4));

    thread1->join();
    thread2->join();
    thread3->join();
    thread4->join();
}

// void *thread_run(void *args)
// {
//     std::string work_type = static_cast<const char *>(args);
//     while (true)
//     {
//         std::cout << "我是一个新线程，我正在做：" << work_type << std::endl;
//         sleep(1);
//     }
// }

// int main()
// {
//     std::unique_ptr<Thread> thread1(new Thread(thread_run, (void *)"hellothread", 1));
//     std::unique_ptr<Thread> thread2(new Thread(thread_run, (void *)"countthread", 2));
//     std::unique_ptr<Thread> thread3(new Thread(thread_run, (void *)"logthread", 3));

//     thread1->join();
//     thread2->join();
//     thread3->join();
// }

// int main()
// {
//     std::unique_ptr<Thread> thread1(new Thread(thread_run,(void*)"hellothread",1));
//     std::unique_ptr<Thread> thread2(new Thread(thread_run,(void*)"countthread",2));
//     std::unique_ptr<Thread> thread3(new Thread(thread_run,(void*)"logthread",3));

//     thread1->start();
//     thread2->start();
//     thread3->start();

//     thread1->join();
//     thread2->join();
//     thread3->join();
// }

// __thread int g_val = 100;

// // 获取线程id
// std::string changeId(const pthread_t &thread_id)
// {
//     char tid[128];
//     snprintf(tid, sizeof(tid), "0x%x", thread_id);
//     return tid;
// }

// void *start_routine(void *args)
// {
//     std::string threadname = static_cast<const char *>(args);
//     // pthread_detach(pthread_self());//设置自己为分离状态

//     int cnt = 5;
//     while (true)
//     {
//         std::cout << threadname << " running... " << changeId(pthread_self()) << " g_val: " << g_val << " &g_val " << &g_val << std::endl;
//         g_val++;
//         sleep(1);

//     }
//     return nullptr;
// }

// int main()
// {
//     pthread_t tid;
//     pthread_create(&tid, nullptr, start_routine, (void *)"thread 1");
//     std::string main_id = changeId(pthread_self());
//     pthread_detach(tid);
//     std::cout << "main thread running ... new thread id: " << changeId(tid) << " main thread id: " << main_id << std::endl;

//     // sleep(2);
//     // 一个线程默认是joinable的，如果设置了分离状态，不能进行等待了
//     //  int n = pthread_join(tid, nullptr);
//     //  std::cout << "result: " << n << " : " << strerror(n) << std::endl;
//     while (true)
//     {
//         std::cout << "main thread running ... new thread id: " << changeId(tid) << " main thread id: " << main_id << " g_val: " << g_val << " &g_val " << &g_val << std::endl;
//         sleep(1);
//     }

//     return 0;
// }